package com.jz.community.moduleshopping.cardGoods.adapter;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jz.community.basecomm.constant.Constant;
import com.jz.community.basecomm.routerUtils.RouterIntentConstant;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.moduleshopping.R;
import com.jz.community.moduleshopping.cardGoods.info.CardHomeInfo;
import com.jz.community.moduleshopping.cardGoods.ui.CardClassifyGoodsActivity;

import java.util.List;

/**
 * Created by Tony.Fan on 2018/8/16 11:01
 */
public class CardHomeAdapter extends BaseQuickAdapter<CardHomeInfo.EmbeddedBeanXX.ContentBean, BaseViewHolder> {

    private CardClassifyGoodsAdapter cardClassifyGoodsAdapter;

    public CardHomeAdapter(@Nullable List<CardHomeInfo.EmbeddedBeanXX.ContentBean> data) {
        super(R.layout.module_shopping_card_home_item_layout, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, CardHomeInfo.EmbeddedBeanXX.ContentBean item) {
        handleCardClassifyGoodsAdapter(helper, item);
    }

    private void handleCardClassifyGoodsAdapter(BaseViewHolder helper, CardHomeInfo.EmbeddedBeanXX.ContentBean item) {
        TextView card_classify_name_tv = helper.getView(R.id.card_classify_name_tv);
        card_classify_name_tv.setText(item.getName());
        RecyclerView card_classify_recyclerView = helper.getView(R.id.card_classify_recyclerView);

        List<CardHomeInfo.EmbeddedBeanXX.ContentBean.CardGoodsInfo> cardGoodsInfo = item.getGoods();
        if (!Preconditions.isNullOrEmpty(cardGoodsInfo) && cardGoodsInfo.size() > 9) {
            CardHomeInfo.EmbeddedBeanXX.ContentBean.CardGoodsInfo cardItemInfo = new CardHomeInfo.EmbeddedBeanXX.ContentBean.CardGoodsInfo();
            cardItemInfo.setLoadMoreRes(R.mipmap.card_most_more);
            cardGoodsInfo.add(cardItemInfo);
        }
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false);
        card_classify_recyclerView.setLayoutManager(layoutManager);
        card_classify_recyclerView.setHasFixedSize(true);
        card_classify_recyclerView.setFocusable(false);
        cardClassifyGoodsAdapter = new CardClassifyGoodsAdapter(cardGoodsInfo);
        card_classify_recyclerView.setAdapter(cardClassifyGoodsAdapter);
        cardClassifyGoodsAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (cardGoodsInfo.size() > 9) {
                    if (position == cardGoodsInfo.size() - 1) {
                        mContext.startActivity(new Intent(mContext, CardClassifyGoodsActivity.class)
                                .putExtra("titleName", item.getName())
                                .putExtra("showCategoryId", item.getId() + ""));
                    }
                } else {
                    ARouter.getInstance()
                            .build(RouterIntentConstant.MODULE_SHOPPING_GOODS_DETAIL)
                            .withString("url", Constant.GOODS_DETAIL_URL + item.getGoods().get(position).getId())
                            .withInt("goodFrom", 0)
                            .navigation();
                }
            }
        });
        helper.addOnClickListener(R.id.card_classify_layout);
    }

    @Override
    public int getItemCount() {
        return super.getItemCount();
    }
}
